import SparkMD5 from 'spark-md5'
// import mediarequest from '@/utils/media-request'

/*export function submitPostUploadFile(formData) {
  return mediarequest({
    url: '/console/cloudstorage/image/upload',
    method: 'post',
    data: formData
  })
}*/

export const getFileFid = (file) => {
  return new Promise((resolve, reject) => {
    let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice
    let chunkSize = 2097152
    let chunks = Math.ceil(file.size / chunkSize)
    let currentChunk = 0

    const spark = new SparkMD5.ArrayBuffer()
    const frOnload = (e) => {
      spark.append(e.target.result)
      currentChunk++
      if (currentChunk < chunks) {
        loadNext()
      } else {
        resolve(spark.end())
      }
    }
    const frOnerror = () => {
      reject('')
    }
    const loadNext = () => {
      const fileReader = new FileReader()
      fileReader.onload = frOnload
      fileReader.onerror = frOnerror
      let start = currentChunk * chunkSize
      let end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize
      fileReader.readAsArrayBuffer(blobSlice.call(file.raw, start, end))
    }
    loadNext()
  })
}

/*export function doUploadFile(options) {
  // 分片大小
  let chunkSize = 2097152
  // 文件信息
  const fileinfo = options.upfile
  // 总片数
  let chunks = Math.ceil(fileinfo.size / chunkSize)
  // 当前分片索引
  let currentChunk = 0
  // 文件切片后的数组
  let chunkList = []
  let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice
  for (let i = 0; i < fileinfo.size; i += chunkSize) {
    let tmp = blobSlice.call(fileinfo.raw, i, Math.min(fileinfo.size, (i + chunkSize)))
    chunkList.push(tmp)
  }
  const promises = chunkList.map(function (item) {
    const formData = new FormData()
    formData.append('chunks', chunks)
    formData.append('currentChunk', currentChunk)
    formData.append('upfile', item)
    formData.append('path', options.path)
    formData.append('fid', options.fid)
    return submitPostUploadFile(formData)
  })
  Promise.all(promises).then(datas => {
    console.log('success' + datas);
  }).catch(errors => {
    console.log('error' + errors);
  })
}*/
